package com.study.spmongodb.im.service;

/**
 * @author study
 * @version 1.0
 * @date 2020/10/8 0:09
 */

import com.study.spmongodb.im.dao.MessageDAO;
import com.study.spmongodb.im.pojo.Message;
import com.study.spmongodb.im.pojo.MessageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    private MessageDAO messageDAO;

    public List<Message> queryMessageList(Long fromId, Long toId, Integer page, Integer rows) {
        final List<Message> list = messageDAO.findListByFromAndTo(fromId, toId, page, rows);
        list.stream().parallel().forEach(message -> {
            if (message.getStatus().intValue() == 1) {
                // 修改消息状态为已读
                this.messageDAO.updateMessageState(message.getId(), MessageStatus.READED);
            }
        });

        return list;
    }
}
